function res = anifun(theta,phi,t,R,L1, L2,M1,M2,g)
K = trebufunV2(theta,phi,t,R,L1, L2,M1,M2,g);
% K = Stuff;
time = K(:,1);
theta = K(:,2);
phi = K(:,3);
thetadot=K(:,4);
phidot=K(:,5);
L1positionx = cos(theta)*L1;
L1positiony = sin(theta)*L1;
Rpositionx = cos(theta)*-R;
Rpositiony = sin(theta)*-R;
L2positionx = L1positionx + cos(phi)*L2;
L2positiony = L1positiony + sin(phi)*L2;
maxX=max([L2positionx;L1positionx;Rpositionx]);
minX=min([L2positionx;L1positionx;Rpositionx]);
maxY=max([L2positiony;L1positiony;Rpositiony]);
minY=min([L2positiony;L1positiony;Rpositiony]);
maxX=max(maxX,-minX);
maxY=max(maxY,-minY);
height=L1+L2;
figure('Position',[0,0,800*maxX*2/(height+maxY),800],'Color',[0 1 1])

% maxDim=max([maxX height+maxY])
xlim([-maxX maxX]);
ylim([0 height+maxY]);
i=1;
line([0,height/2],[height,0],'LineWidth',4,'Color',[184/255;135/255;59/255]);
 line([0,-height/2],[height,0],'LineWidth',4,'Color',[184/255;135/255;59/255]);
 line([height/4,-height/4],[height/2,height/2],'LineWidth',4,'Color',[184/255;135/255;59/255]);
beam=line([Rpositionx(i),L1positionx(i)],[Rpositiony(i)+height,L1positiony(i)+height],'LineWidth',2,'Color',[139/255;69/255;19/255]);
sling=line([L2positionx(i),L1positionx(i)],[L2positiony(i)+height,L1positiony(i)]+height);
pd=M2/(M1+M2)*2;
cd=M1/(M1+M2)/2;
projectile=rectangle('Position',[L2positionx(i)-pd/2,L2positiony(i)-pd/2+height,pd,pd],'Curvature',[.8 .5],'FaceColor',[1,0,0]);
counterweight=rectangle('Position',[Rpositionx(i)-cd/2,Rpositiony(i)-cd/2+height,cd,cd],'Curvature',[.1 .1],'FaceColor',[0 1 0]);
%     axis(

title('Trebuchet Position')
hold on
%hinge
 plot(0,0+height,'o','MarkerEdgeColor',[0,0,0],'MarkerSize',10,'LineWidth',2);
 
for i = 2:length(time)
     pause((time(i)-time(i-1))/10);
    set(beam,'XData',[Rpositionx(i), L1positionx(i)]);
    set(beam,'YData',[Rpositiony(i)+height,L1positiony(i)+height ]);
    set(sling,'XData',[L2positionx(i),L1positionx(i)]);
    set(sling,'YData',[L2positiony(i)+height,L1positiony(i)+height ]);
    set(projectile,'Position',[L2positionx(i)-pd/2,L2positiony(i)-pd/2+height,pd,pd]);
    set(counterweight,'Position',[Rpositionx(i)-cd/2,Rpositiony(i)-cd/2+height,cd,cd]);
% plot(,, 'ro','MarkerSize',12);
% plot(L2positionx(i),L2positiony(i), 'bo','MarkerSize',12);
% plot(Rpositionx(i),Rpositiony(i), 'yo','MarkerSize',12);
% plot([L1positionx(i),Rpositionx(i)],[L1positiony(i),Rpositiony(i)],'g');
% plot([L1positionx(i),L2positionx(i)],[L1positiony(i),L2positiony(i)],'k');
%  res(i) = getframe;

% legend('Sling Arm','Sling','Counterweight')
end
m=projectilepositions(L1, L2, theta(end), phi(end), thetadot(end), phidot(end), height);
time=m(:,1); x=m(:,2); y=m(:,3);
pmaxX=max([maxX x']);
xlim( [-maxX pmaxX]   )
ylim([0 (height+maxY)*(1+(pmaxX-maxX)/(2*maxX))])
pd=pd*10;
for i=2:length(x)
    pause(time(i)-time(i-1));
 set(projectile,'Position',[x(i)-pd/2,y(i)-pd/2+height,pd,pd]);
end
% movie(res,1,10)
end
%(200,0,280,0,2,10,5,10000,.1,4,-9.8)
% movie(M,n,fps)
% M - stored frames
% n - loops, there are additional options with this parameter
% fps - frames per second